/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flightlifter;

import java.util.Vector;
import org.lwjgl.opengl.GL11;

/**
 *
 * @author Matt
 */
public class ConstantNode extends WiringNode implements WiringOutput {

    private float value = 1.f;
    private WiringConsumer consumer = null;
    private int type;
    private int consumerIndex;

    public void setConsumer(WiringConsumer consumer) {
        this.consumer = consumer;
        System.out.println("Set consumer:" + consumer);
    }

    public void invalidate() {
        finished = false;
    }

    public ConstantNode(int x, int y, TrueTypeFont font) {
        this.font = font;
        locX = x;
        locY = y;
    }

    public void evaluate() {
        //don't do anything until our parent node has set our value
        //if no output, then don't wait for this node
        if (consumer == null) {
            finished = true;
        } else {
            consumer.setValue(value);
            finished = true;
        }

    }

    public void lineToConsumer() {

        if (consumer != null) {
            PVector center = consumer.getScreenCenter();

            GL11.glColor3f(0, 0, 1);

            GL11.glPushMatrix();

            GL11.glMatrixMode(GL11.GL_PROJECTION);                        // Select The Projection Matrix
            GL11.glLoadIdentity();                                   // Reset The Projection Matrix
            GL11.glOrtho(0, 1280, 0, 1024, -1, 1);                          // Set Up An Ortho Screen
            GL11.glMatrixMode(GL11.GL_MODELVIEW);                         // Select The Modelview Matrix
            GL11.glLoadIdentity();                                   // Reset The Modelview Matrix

            GL11.glBegin(GL11.GL_LINES);
            GL11.glVertex3f(locX + cWidth / 2, locY, 0);

            //connect nodes at the edges. For objects on the planes, go right to the center
            if (consumer instanceof WiringNode) {
                GL11.glVertex3f(center.x - cWidth / 2, center.y, 0);
            } else {
                GL11.glVertex3f(center.x, center.y, 0);
            }
            GL11.glEnd();
            GL11.glPopMatrix();
        }

    }

    public int mousePressed(int mouseX, int mouseY) {


        //check if mouse pressed on this object
        if (mouseX > locX && mouseX < locX + cWidth) {
            if (mouseY < locY + cHeight && mouseY > locY) {
                System.out.println("clicked on constant node");
               // selectSet.add(this);
                return 1;
            }

        }
        return 0;


    }





    public void hookUp(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        System.out.println("Hooking up " + type + ":" + consumerIndex);
        //-1 means this node wasn't hooked up to anything on save
        if (consumerIndex != -1) {
            if (type == 0) {
                setConsumer((WiringConsumer) wiring.elementAt(consumerIndex));
            } else {
                setConsumer((WiringConsumer) nodes.elementAt(consumerIndex));
            }
        }
    }

    public ConstantNode(String[] line, TrueTypeFont font) {
        this(Integer.parseInt(line[1]),
                Integer.parseInt(line[2]),
                font);

        type = Integer.parseInt(line[3]);
        consumerIndex = Integer.parseInt(line[4]);
        value = Float.parseFloat(line[5]);
    }

    public String pickle(Vector<Node> nodes,
            Vector<Node> constraints,
            Vector<WiringNode> wiring) {

        String ret = ","
                + locX + ","
                + locY + ",";

        if (consumer instanceof WiringNode) {
            ret += 0 + "," + wiring.indexOf(consumer);
        } else {
            ret += 1 + "," + constraints.indexOf(consumer);
        }
        ret += value + ",";
        return ret;
    }

    public void setValue(float value) {
        this.value = value;
    }


}
